package com.example.onlineshop.Helper;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.onlineshop.Activity.MainActivity;
import com.example.onlineshop.Adapter.CartAdapter;
import com.example.onlineshop.Domain.CartEntity;
import com.example.onlineshop.R;
import com.example.onlineshop.db.SqliteCarOpenHelper;
import com.example.onlineshop.db.onListener;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class CartsHelper extends AppCompatActivity implements onListener {
    private ImageView backBtn;

    private RecyclerView recyclerViewCat;
    CartAdapter catAdapter;
    SqliteCarOpenHelper dbHelper;
    SQLiteDatabase db;
    List<CartEntity> mList;
    Button button2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_carts_helper);


        backBtn=findViewById(R.id.backBtn);
        button2=findViewById(R.id.button2);
        initRecyclerviewCat();
        backBtn.setOnClickListener(v -> CartsHelper.this.startActivity(new Intent(CartsHelper.this,MainActivity.class)));
    }
    private void initRecyclerviewCat() {

        dbHelper = new SqliteCarOpenHelper(this, "cart.db", null, 1);
        db = dbHelper.getWritableDatabase();
        mList=new ArrayList<>();
        mList= dbHelper.QueryAll();
        double price=0;
        for (int i = 0; i < mList.size(); i++) {
            BigDecimal bigDecimal=new BigDecimal(mList.get(i).getPrice());  //int
            price +=Double.parseDouble(bigDecimal.multiply(new BigDecimal(mList.get(i).getNum()))+"") ;
        }
        button2.setText("結算("+price+")");
        recyclerViewCat = findViewById(R.id.catView);
        recyclerViewCat.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        catAdapter = new CartAdapter(mList);
        recyclerViewCat.setAdapter(catAdapter);
        catAdapter.setListener(this);



    }


    @Override
    public void OnListener(int flag,CartEntity item) {
        Toast.makeText(this, "已更新"+flag, Toast.LENGTH_SHORT).show();
        switch (flag){
            //加
             case 1:
                 item.setNum(item.getNum()+1);
                 dbHelper.updateData(item.getTitle(),item.getImgPath(),item.getPrice(),item.getNum(),item.getRatePrice());
                 break;
                 //減
             case 2:
                 item.setNum(item.getNum()-1);
                 dbHelper.updateData(item.getTitle(),item.getImgPath(),item.getPrice(),item.getNum(),item.getRatePrice());
                 break;
                 //刪除
            case 3:
                dbHelper.deleteData(item.getTitle());
                break;
        }
        mList.clear();
         mList.addAll(dbHelper.QueryAll());
        catAdapter.notifyDataSetChanged();
        double price=0;
        for (int i = 0; i < mList.size(); i++) {
            BigDecimal bigDecimal=new BigDecimal(mList.get(i).getPrice());  //要用 bigDecimal去做加減乘除 用*有問題
            price +=Double.parseDouble(bigDecimal.multiply(new BigDecimal(mList.get(i).getNum()))+"") ;
        }
        button2.setText("結算("+price+")");
    }
}